package com.agranee.timesheet.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.agranee.timesheet.model.Employee;
import com.agranee.timesheet.service.LoginServiceImpl;
import com.agranee.timesheet.serviceable.IEmployeeService;
import com.agranee.timesheet.utility.Util;


@RequestScoped
@ManagedBean(name="loginBean")
public class LoginBean {
	private static final String SUCCESS = "success";
    private static final String ERROR   = "error";
    
    @ManagedProperty("#{loginService}") 
    private IEmployeeService loginService;
    
   
	
	
	private String employeeUserId;
	
	private String employeePassword;	
	
	

	public IEmployeeService getLoginService() {
		return loginService;			
	}

	public void setLoginService(IEmployeeService loginService) {
		this.loginService = loginService;
	}

	

	public String getEmployeeUserId() {
		return employeeUserId;
	}

	public void setEmployeeUserId(String employeeUserId) {
		this.employeeUserId = employeeUserId;
	}

	public String getEmployeePassword() {
		return employeePassword;
	}

	public void setEmployeePassword(String employeePassword) {
		this.employeePassword = employeePassword;
	}

	
	
	public String validateUser(){
		
		
		Employee employee=this.getLoginService().getUserByUserId(getEmployeeUserId());
		if(employee!=null){
			if(employee.getEmployeePassword().equals(Util.encryptInMd5(getEmployeePassword()))){
				
				if(employee.getEmployeeType().equalsIgnoreCase("admin"))
					return "admin_home";
				else if(employee.getEmployeeType().equalsIgnoreCase("ca"))
					return "ca_home";
				else if(employee.getEmployeeType().equalsIgnoreCase("hr"))
					return "hr_home";
				else if(employee.getEmployeeType().equalsIgnoreCase("employee"))
					return "timesheet_generation";
			}
		}
		
		return ERROR;
	}
	
	public String retriveUserId(){
		return null;
		
	}

}
